package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.action.front.TmemberAction;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberbankcard;
import com.xy.p2p.service.TmemberbankcardService;



@Controller
public class TmemberbankcardAction{
	private static final Logger log = LoggerFactory.getLogger(TmemberbankcardAction.class);
	public TmemberbankcardAction() {
	}
 
 
 	/**
	 * 会员银行卡信息表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbankcardAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmemberbankcard", new Tmemberbankcard());
        setDict(model);
		return "tmemberbankcardEdit";
	}

	/**
	 * 会员银行卡信息表新增保存
	 * 
	 * @param tmemberbankcard
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("o_tmemberbankcardSave.do")
	public String save(Tmemberbankcard tmemberbankcard, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmemberbankcard, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmemberbankcard = tmemberbankcardService.save(tmemberbankcard);
		return "redirect:v_tmemberbankcardList2.do";
	}

	/**
	 * 会员银行卡信息表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmemberbankcardDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmemberbankcardService.remove(id);
			}
		}
		return "redirect:v_tmemberbankcardList2.do";
	}

	/**
	 * 会员银行卡信息表编辑页面
	 * 
	 * @param mbcid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbankcardEdit.do")
	public String edit(long mbcid, HttpServletRequest request, ModelMap model) {
		Tmemberbankcard tmemberbankcard = tmemberbankcardService.get(mbcid);
		model.addAttribute("tmemberbankcard", tmemberbankcard);
        setDict(model);
		return "tmemberbankcardEdit";
	}

	/**
	 * 会员银行卡信息表详情
	 * 
	 * @param mbcid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbankcardDetail.do")
	public String detail(long mbcid, HttpServletRequest request, ModelMap model) {
		Tmemberbankcard tmemberbankcard = tmemberbankcardService.get(mbcid);
		model.addAttribute("tmemberbankcard", tmemberbankcard);
        setDict(model);
		return "tmemberbankcardView";
	}

	/**
	 * 会员银行卡信息表查询（分页）
	 * 
	 * @param tmemberbankcard
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbankcardList.do")
	public String listPage(Tmemberbankcard tmemberbankcard, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberbankcardService.getTmemberbankcardByPage(toMap(tmemberbankcard),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmemberbankcardList";
	}

	/**
	 * 会员银行卡信息表查询
	 * 
	 * @param tmemberbankcard
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbankcardList1.do")
	public String list(Tmemberbankcard tmemberbankcard, HttpServletRequest request, ModelMap model) {
		List<Tmemberbankcard> list = tmemberbankcardService.getTmemberbankcard(toMap(tmemberbankcard));
		model.addAttribute("list", list);
        setDict(model);
		return "tmemberbankcardList";
	}

	
	/*
	 *查询
	 * */
	@RequestMapping("v_tmemberbankcardList2.do")
	public String list(Tmemberbankcard tmemberbankcard, Integer pageNo,HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberbankcardService.getTmemberbankcardByPage(toMap(tmemberbankcard),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "cardInfo";
	}
	/*
	 * 修改
	 * */
	@RequestMapping("o_tmemberbankcardEdit2.do")
	public String toAdd(HttpServletRequest request, ModelMap model) {
		String id = null;
		setDict(model);
		Tmemberbankcard tmemberbankcard = new Tmemberbankcard();
		Tmember tmember = (Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		tmemberbankcard.setTmember(tmember);
		if (null == request.getParameter("id")){
			model.addAllAttributes(toMap(tmemberbankcard));
			return "cardInfoEdit";
		}
		id = request.getParameter("id");
		tmemberbankcard.setMbcid(Long.parseLong(id));
		List<Tmemberbankcard> list= tmemberbankcardService.getTmemberbankcard(toMap(tmemberbankcard));
		model.addAllAttributes(toMap(list.get(0)));
		return "cardInfoEdit";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmemberbankcard
	 * @return
	 */	private Map<String,Object> toMap(Tmemberbankcard tmemberbankcard){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmemberbankcard.getMbcid()!=null&&!tmemberbankcard.getMbcid().equals("")){
			map.put("mbcid", tmemberbankcard.getMbcid());
		}
		if(tmemberbankcard.getTmember()!=null&&!tmemberbankcard.getTmember().equals("")){
			map.put("Tmember", tmemberbankcard.getTmember());
		}
		if(tmemberbankcard.getBank()!=null&&!tmemberbankcard.getBank().equals("")){
			map.put("bank", tmemberbankcard.getBank());
		}
		if(tmemberbankcard.getUsername()!=null&&!tmemberbankcard.getUsername().equals("")){
			map.put("username", tmemberbankcard.getUsername());
		}
		if(tmemberbankcard.getBankname()!=null&&!tmemberbankcard.getBankname().equals("")){
			map.put("bankname", tmemberbankcard.getBankname());
		}
		if(tmemberbankcard.getBanknameaddr()!=null&&!tmemberbankcard.getBanknameaddr().equals("")){
			map.put("banknameaddr", tmemberbankcard.getBanknameaddr());
		}
		if(tmemberbankcard.getBankcard()!=null&&!tmemberbankcard.getBankcard().equals("")){
			map.put("bankcard", tmemberbankcard.getBankcard());
		}
		if(tmemberbankcard.getIswithdraw()!=null&&!tmemberbankcard.getIswithdraw().equals("")){
			map.put("iswithdraw", tmemberbankcard.getIswithdraw());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		model.addAttribute("iswithdrawMap", DictUtils.getDictByType(DictUtils.TMEMBERBANKCARD_ISWITHDRAW));
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmemberbankcard tmemberbankcard, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmemberbankcardService tmemberbankcardService;   
}